package mypodsync;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import javax.swing.JPanel;

public class GeneralConfig implements Config {

    private GeneralConfigPanel panel;
    private Preferences prefs;
    private final String USE_PROXY = "use_proxy";
    private final String PROXY_HOST = "proxy_host";
    private final String PROXY_PORT = "proxy_port";
    private final String USER_NAME = "user_name";
    private final String USER_PASS = "user_pass";
    private Logger log = Logger.getLogger(GeneralConfig.class.getName());
    
    public GeneralConfig() {
        panel = new GeneralConfigPanel();
        prefs = Preferences.userNodeForPackage(getClass());
    }
    
    public JPanel getUI() {
        return panel;
    }

    public String getTitle() {
        return "General";
    }

    public void load() {
        panel.setUseProxy(useProxy());
        panel.setServer(getProxyHost());
        panel.setPort(Integer.toString(getProxyPort()));
        panel.setUserName(getUserName());
        panel.setUserPass(getPassword());
    }

    public char[] getPassword() {
        return prefs.get(USER_PASS, "").toCharArray();
    }

    public String getUserName() {
        return prefs.get(USER_NAME, "");
    }

    public int getProxyPort() {
        String p = prefs.get(PROXY_PORT, "0");
        if (p.equals("")) {
            return 0;
        } else {
           return Integer.parseInt(prefs.get(PROXY_PORT, "0"));
        }
    }

    public String getProxyHost() {
        return prefs.get(PROXY_HOST, "");
    }

    public boolean useProxy() {
        return prefs.getBoolean(USE_PROXY, false);
    }

    public void save() {
        prefs.putBoolean(USE_PROXY, panel.getUseProxy());
        prefs.put(PROXY_HOST, panel.getServer());
        prefs.put(PROXY_PORT, panel.getPort());
        prefs.put(USER_NAME, panel.getUserName());
        prefs.put(USER_PASS, new String(panel.getUserPass()));
        try {
            prefs.flush();
        } catch (BackingStoreException ex) {
            log.log(Level.SEVERE, ex.getMessage(), ex);
        }
    }

 
    
}
 
